<?
////////////////////////////////////////////////////////////////////////////////
//   Copyright (C) ReloadCMS Development Team                                 //
//   http://reloadcms.sf.net                                                  //
//                                                                            //
//   This program is distributed in the hope that it will be useful,          //
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
//                                                                            //
//   This product released under GNU General Public License v2                //
//////////////////////////////////////////////////////////////////////////////// 
//config section

$picload_path=DATA_PATH.'picload/';
$picture_size="2000000";
$guest_load="true";
$picloadsiteurl="http://localhost/devel/1.2.x-stable/";

/////////////////////////////
//Picload API functions  ////
////////////////////////////


// Function that returns picture requirements
function show_pic_requirements()
{
global $picload_path;
global $picture_size;
$requirements=__('Your picture must be ').$picture_size.__(' bytes in size and have type png, jpg or gif');
return $requirements;
}

// Function to upload picture 
function upload_pic()
{
global $picload_path, $picloadsiteurl;
if ((isset($_POST['upload_pic'])) AND ($_POST['upload_pic']=="true") AND (is_image($_FILES['picload']['name'])))
{
$uploadfile = $picload_path . basename($_FILES['picload']['name']);
if ((move_uploaded_file($_FILES['picload']['tmp_name'], $uploadfile))) {
   show_window(__('Result'),__('Picture sucefully uploaded'),'center');
   $picinscode=__('BB code').'<br>
   <textarea>[img]'.$picloadsiteurl.$picload_path.$_FILES['picload']['name'].'[/img]</textarea><br>
   '.__('HTML code').'<br>
   <textarea><img src="'.$picloadsiteurl.$picload_path.$_FILES['picload']['name'].'"></textarea><br>
   '.__('Picture preview').'<br>
   <img src="'.$picloadsiteurl.$picload_path.$_FILES['picload']['name'].'">
   ';
   show_window(__('Picture insertion code'),$picinscode,'center');
   return $_FILES['picload']['name'];
} else {
show_window(__('Result'),__('Your picture don\'t meet our requirements'),'center');
}
}
} 

// Function that shows form to upload picture
function pic_upload_box()
{
global $picture_size;
$form='<form enctype="multipart/form-data" action="" method="POST">
		<input type="hidden" name="upload_pic" value="true">
       <input type="hidden" name="MAX_FILE_SIZE" value="'.$picture_size.'" />
       '.__('Select picture from your HDD').': <input name="picload" type="file"/>
    <input type="submit" value="'.__('Upload picture').'" />
</form>';
return $form;
}  


//function for check image type
function is_image($filename)
{
global $picload_path;
print($picload_path.$filename);
//print($picload_path.$filename);
if (file_exists($picload_path.$filename))
{
show_window(__('Error'),__('Your file is not an image or is corrupted or such image already exists'));
return false;
}
$ext=strtolower(substr(strrev($filename),0,4));	
if (($ext=='gpj.') OR ($ext=='gnp.') OR ($ext=='fig.') OR ($ext=='gepj') OR ($ext=='GPJ.') OR ($ext=='GNP.') OR ($ext=='FIG.') OR ($ext=='GEPJ'))
{
	return true;
}
else 
{
	show_window(__('Error'),__('Your file is not an image or is corrupted or such image already exists'));
	return false;
}
} 

//////////////////////////// Module main code ////////////////////////////

show_window('',__('With this module you can upload pictures to server for using they into your articles or forum posts'),'left');
if ((isset($_POST['upload_pic'])) AND ($_POST['upload_pic']=="true"))
{
upload_pic();

}
show_window(__('Upload picture to server'),show_pic_requirements().'<br>'.pic_upload_box(),'center');
?>